/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年6月12日
 */
package com.massyframework.beanskin.graphql.servlet.provider;

import java.net.URL;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration;

import com.massyframework.beanskin.Environment;
import com.massyframework.beanskin.Module;
import com.massyframework.beanskin.ModuleIdentifier;
import com.massyframework.beanskin.SimpleModuleIdentifier;
import com.massyframework.beanskin.framework.Framework;
import com.massyframework.beanskin.graphql.servlet.GraphQLModularHttpServlet;
import com.massyframework.beanskin.graphql.servlet.GraphQLModularSchemalBuilder;
import com.massyframework.beanskin.plugin.Plugin;
import com.massyframework.beanskin.plugin.PluginActivator;
import com.massyframework.beanskin.util.version.Version;

/**
 * GraphQL插件激活器
 * @author huangkh
 *
 */
public final class GraphQLPluginActivator implements PluginActivator {
	
	private Framework framework;
	private GraphQLModularSchemalBuilder builder;
	
	/**
	 * 
	 */
	public GraphQLPluginActivator() {
	}

	@Override
	public void init(Environment<Plugin> env) throws Exception {
		if (env.runAtJ2EE()) {
			this.registGraphQLServlet(env);
			this.framework = env.getFramework();
		}
	}
	
	@Override
	public void destroy() {
		if (this.framework != null) {
			if (this.builder != null) {
				this.framework.removeListener(this.builder);
				this.builder = null;
			}
			this.framework = null;
		}
	}

	/**
	 * 注册Servlet
	 * @param env {@link Environment}
	 */
	protected void registGraphQLServlet(Environment<Plugin> env)  throws Exception{		
		ModuleIdentifier identifier =
				SimpleModuleIdentifier.newBuilder()
					.groupId("com.massyframework.beanskin")
					.moduleId("graphql-context")
					.version(Version.valueOf("2.0.0"))
					.build();
		Module<?> module = env.getFramework().findModuleByIdentifier(identifier);
		if (module != null) {						
			javax.servlet.ServletContext context =
					env.getKernelAssembly().getAssemblyContext()
						.getBean(javax.servlet.ServletContext.class.getName(), javax.servlet.ServletContext.class);
			URL url = module.getModuleClassLoader().getResource("BEANSKIN-INF/config/domain.graphql");
			this.builder = new GraphQLModularSchemalBuilder(env.getModule(), url);
			GraphQLModularHttpServlet servlet = new GraphQLModularHttpServlet(builder);
					
			ServletRegistration.Dynamic registration = context.addServlet("graphQLServlet", servlet);
			registration.setMultipartConfig(new MultipartConfigElement(""));
			registration.addMapping("/graphql/*");
			registration.setAsyncSupported(true);

			context.addListener(new StartListener(builder));
			env.getFramework().addListener(builder);
		}		
	}
}
